package com.kinon.ycjshop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.kinon.ycjshop.R;
import com.kinon.ycjshop.sqlbean.BuyGoodsBean;

import java.util.ArrayList;

/**
 * Created by luohao on 2017-08-09.
 */

public class UrgentBuyAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<BuyGoodsBean> list;

    private OnAdapterClickListener listener = null;

    public UrgentBuyAdapter(Context context, ArrayList<BuyGoodsBean> list) {
        super();
        this.context = context;
        this.list = list;
    }

    public void setAdapterClickListener(OnAdapterClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHandle handle;
        if (view == null) {
            handle = new ViewHandle();
            view = LayoutInflater.from(context).inflate(R.layout.item_shangpin, null);
            handle.tv_tiaoma = (TextView) view.findViewById(R.id.tv_tiaoma);
            handle.tv_name = (TextView) view.findViewById(R.id.tv_name);
            handle.tv_jiage = (TextView) view.findViewById(R.id.tv_jiage);
            handle.ib_jia = (ImageButton) view.findViewById(R.id.ib_jia);
            handle.ib_jian = (ImageButton) view.findViewById(R.id.ib_jian);
//            handle.et_amount = (EditText) view.findViewById(R.id.et_amount);
            handle.tv_amount2 = view.findViewById(R.id.tv_amount2);
            handle.tv_xiaoji = (TextView) view.findViewById(R.id.tv_xiaoji);

//				handle.tv_sname=(TextView)view.findViewById(R.id.tv_sname);
            view.setTag(handle);
        } else {
            handle = (ViewHandle) view.getTag();
        }

        handle.tv_tiaoma.setText(list.get(position).getCode());
        handle.tv_name.setText(list.get(position).getName());
//        handle.et_amount.setText(list.get(position).getAmount() + "");
        handle.tv_amount2.setText(list.get(position).getAmount() + "");
        handle.tv_jiage.setText("￥" + list.get(position).getPrice());
        handle.tv_xiaoji.setText("￥" + list.get(position).getAmount() *
                list.get(position).getPrice());
        handle.ib_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, 1);
                }
            }
        });
        handle.ib_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, 0);
                }
            }
        });

        return view;
    }

    public interface OnAdapterClickListener {
        void onItemClick(int position, int button);
    }

    class ViewHandle {
        /**
         * 条码
         */
        TextView tv_tiaoma;
        /**
         * 商品名
         */
        TextView tv_name;
        /**
         * 加
         */
        ImageButton ib_jia;
        /**
         * 数量
         */
//        EditText et_amount;
        TextView tv_amount2;
        /**
         * 减
         */
        ImageButton ib_jian;
        /**
         * 价格
         */
        TextView tv_jiage;
        /**
         * 小计
         */
        TextView tv_xiaoji;
    }

}
